package com.myblog.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.myblog.entity.Article;
import com.myblog.entity.BlogMessage;
import com.myblog.mapper.BlogMessageMapper;
import com.myblog.service.BlogMessageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.myblog.utils.PageResult;
import com.myblog.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lhj
 * @since 2022-05-25
 */
@Service
public class BlogMessageServiceImpl extends ServiceImpl<BlogMessageMapper, BlogMessage> implements BlogMessageService {

    @Autowired
    private BlogMessageMapper blogMessageMapper;

    @Override
    public PageResult findPage(Integer pageSize, Integer pageNum) {
        PageResult pageResult = PageUtils.getPageResult(getPageInfo(pageSize, pageNum));
        return pageResult;
    }

    @Override
    public int addMessage(BlogMessage blogMessage) {
        // 默认游客id留言，后期添加用户后更改
        blogMessage.setUid(4);
        int insert = blogMessageMapper.insert(blogMessage);
        return insert;
    }

    private PageInfo<?> getPageInfo(Integer pageNum, Integer pageSize){
        PageHelper.startPage(pageSize, pageNum);
        // 获取分页查询后得数据
//        List<BlogMessage> blogMessages = blogMessageMapper.findMessageAll();
        List<BlogMessage> blogMessages = blogMessageMapper.findMessageUserAll();
        PageInfo<BlogMessage> messagesPageInfo = new PageInfo<>(blogMessages, pageNum);
        return messagesPageInfo;
    }


}
